<template>
  <div 
    class="compact-school-item"
    :class="[`layout-${layout}`]"
    @click="$emit('click', school)"
  >
    <div class="school-logo-container">
      <el-avatar 
        :size="layout === 'vertical' ? 60 : 40" 
        :src="processedLogo" 
        fit="cover"
        class="school-logo"
      >
        <el-icon><School /></el-icon>
      </el-avatar>
    </div>
    <div class="school-content">
      <h4 class="school-name">{{ school.name }}</h4>
      <div class="school-stats">
        <el-progress 
          :percentage="getProgressPercentage()" 
          :color="getColorMethod()"
          :show-text="false"
          :stroke-width="5"
          class="school-progress"
        />
        <p class="school-count">{{ school.productCount }}件商品</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { defineProps, defineEmits, computed, ref } from 'vue';
import { School } from '@element-plus/icons-vue';

const props = defineProps({
  school: {
    type: Object,
    required: true
  },
  layout: {
    type: String,
    default: 'horizontal', // horizontal, vertical
  },
  maxCount: {
    type: Number,
    default: 1500
  }
});

// 处理学校logo，确保有fallback
const defaultAvatar = 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png';

const processedLogo = computed(() => {
  if (!props.school.logo) {
    return defaultAvatar;
  }
  
  // 如果已经是完整URL则直接返回
  if (props.school.logo.startsWith('http://') || props.school.logo.startsWith('https://')) {
    return props.school.logo;
  }
  
  // 如果是相对路径，需要检查是否有前缀
  if (props.school.logo.startsWith('/')) {
    return import.meta.env.VITE_API_BASE_URL + props.school.logo;
  }
  
  // 如果只是文件名，也加上前缀
  return import.meta.env.VITE_API_BASE_URL + '/' + props.school.logo;
});

const getProgressPercentage = () => {
  return Math.min(100, (props.school.productCount / props.maxCount) * 100);
};

const getColorMethod = () => {
  const percentage = getProgressPercentage();
  if (percentage < 30) {
    return '#909399';
  } else if (percentage < 70) {
    return '#e6a23c';
  } else {
    return '#67c23a';
  }
};

defineEmits(['click']);
</script>

<style scoped>
.compact-school-item {
  background-color: #fff;
  border-radius: 10px;
  overflow: hidden;
  transition: all 0.3s;
  cursor: pointer;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.06);
  height: 100%;
}

.compact-school-item:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
}

/* 水平布局 */
.layout-horizontal {
  display: flex;
  align-items: center;
  padding: 12px;
}

.layout-horizontal .school-logo-container {
  margin-right: 15px;
}

.layout-horizontal .school-content {
  flex: 1;
}

/* 垂直布局 */
.layout-vertical {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 15px;
}

.layout-vertical .school-logo-container {
  margin-bottom: 15px;
}

.layout-vertical .school-content {
  width: 100%;
  text-align: center;
}

/* 通用样式 */
.school-logo {
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  border: 3px solid white;
  transition: transform 0.3s;
}

.compact-school-item:hover .school-logo {
  transform: scale(1.1);
}

.school-name {
  margin: 0 0 8px;
  font-size: 15px;
  font-weight: 600;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.school-stats {
  display: flex;
  flex-direction: column;
}

.layout-vertical .school-stats {
  align-items: center;
}

.school-progress {
  margin-bottom: 5px;
}

.layout-horizontal .school-progress {
  max-width: 120px;
}

.layout-vertical .school-progress {
  width: 80%;
}

.school-count {
  margin: 0;
  font-size: 12px;
  color: #666;
}
</style> 